package com.ww.scm.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ww.core.orm.Page;
import com.ww.core.orm.PropertyFilter;
import com.ww.core.orm.PropertyFilter.MatchType;
import com.ww.core.utils.mapper.JsonMapper;
import com.ww.scm.constants.Constants;
import com.ww.scm.entity.mat.Bill;
import com.ww.scm.entity.mat.FinanceWastebook;
import com.ww.scm.entity.mat.TransactionDetail;
import com.ww.scm.service.StorageManager;
import com.ww.scm.service.WastebookManager;
import com.ww.scm.valueObject.croe.ResponseEntity;
import com.ww.scm.valueObject.croe.ResponseInfo;
import com.ww.scm.valueObject.croe.ResponseJson;
import com.ww.scm.valueObject.mat.BillDetailVo;
import com.ww.scm.valueObject.mat.BillVo;
import com.ww.scm.valueObject.mat.FinanceWastebookVo;

@Controller
@RequestMapping(value = "/inacount")
public class InAcountController {

	private static Logger logger = LoggerFactory.getLogger(PurchaseController.class);
	@Autowired
	private WastebookManager wastebookManager;
	
	public void setWastebookManager(WastebookManager wastebookManager) {
		this.wastebookManager = wastebookManager;
	}
	
	@RequestMapping(params = "method=list")
	@ResponseBody
	public String list(HttpServletRequest request) {
		String limit=request.getParameter("limit");
		String start=request.getParameter("start");

		Page<FinanceWastebook> page = new Page<FinanceWastebook>(start,limit);
		
		
		 
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
		filters.add(new PropertyFilter(MatchType.EQ,"sysFlag",Constants.SYS_FLAG_USEING));
		page = wastebookManager.findFinanceWastebook(page, filters);
		List<FinanceWastebookVo> returnList=new ArrayList<FinanceWastebookVo>();
		for (FinanceWastebook financeWastebook : page.getResult()) {
			FinanceWastebookVo financeWastebookVo=new FinanceWastebookVo(financeWastebook);   
			returnList.add(financeWastebookVo);
		}
		ResponseEntity<FinanceWastebookVo> responseEntity=new ResponseEntity<FinanceWastebookVo>();
		responseEntity.setEntity("financeWastebookVo", returnList);
		responseEntity.setMaxRecordSizeValue(page.getTotalItems());
		return responseEntity.toJson();
	}
	

    @RequestMapping(params = "method=delete")
    @ResponseBody
    public String delete(String ids) {
    	if (StringUtils.isBlank(ids)){
    		logger.error("删除方法中传入参数ids为空!");
    		ResponseInfo responseInfo=new ResponseInfo(false,"删除方法中传入参数ids为空!");
    		return responseInfo.toJson();
    	}
    	ResponseInfo responseInfo=new ResponseInfo();
    	//todo:这里需要得到登录用户
    	String operator="1001";
    	try {
    		String[] idArray=ids.split(Constants.SEPARATOR_COLON);
            for(int i = 0; i < idArray.length; i++) {
            	wastebookManager.deleteFinanceWastebook(idArray[i], operator);
            }
		} catch (Exception e) {
			logger.error("删除单据出错：",e);
			responseInfo.setResponseContent(e.getMessage());
			responseInfo.setResponseSuccess(false);
		}
		return responseInfo.toJson();
    }
    
    @RequestMapping(params = "method=edit")
    @ResponseBody
    public String edit(String id) {
    	if (StringUtils.isBlank(id)){
    		logger.error("修改方法中传入参数id为空!");
    		ResponseInfo responseInfo=new ResponseInfo(false,"修改方法中传入参数id为空!");
    		return responseInfo.toJson();
    	}
    	FinanceWastebook financeWastebook = wastebookManager.findFinanceWastebookById(id);
		FinanceWastebookVo financeWastebookVo=new FinanceWastebookVo(financeWastebook); 
		ResponseEntity<FinanceWastebookVo> responseEntity=new ResponseEntity<FinanceWastebookVo>();
		responseEntity.setEntity("financeWastebookVo", financeWastebookVo);
        return responseEntity.toJson();
    }
    
    @RequestMapping(params = "method=save")
    @ResponseBody
    public String save(String dataset) {
    	ResponseInfo responseInfo=new ResponseInfo();
    	try {
    	ResponseJson<FinanceWastebookVo> responseJson=new ResponseJson<FinanceWastebookVo>(dataset);
    	FinanceWastebookVo financeWastebookVo=responseJson.toEntity(FinanceWastebookVo.class, "financeWastebookVo"); 
    	wastebookManager.saveFinanceWastebook(financeWastebookVo);
    	} catch (Exception e) {
			logger.error("保存出错：",e);
			responseInfo.setResponseContent(e.getMessage());
			responseInfo.setResponseSuccess(false);
		}
    	return responseInfo.toJson();
    }
    
   /* @RequestMapping(params = "method=randomBillCode")
    @ResponseBody
    public String randomBillCode(String billType) {
    	String billCode=storageManager.randomBillCode(billType);
    	
		ResponseEntity<String> responseEntity=new ResponseEntity<String>();
		responseEntity.setEntity("billCode", billCode);
        return responseEntity.toJson();
    }*/
    
  
	
}
